/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* dummyHead = new ListNode(0);
        ListNode* p = l1;
        ListNode* q = l2;
        ListNode* curr = dummyHead;
        int carry = 0;
        int sum = 0;
        int x , y;
        
        while(p != NULL || q != NULL){
            x = (p != NULL) ? p -> val : 0 , y = (q != NULL) ? q -> val : 0;
            sum = x + y + carry;
            curr -> next = new ListNode(sum % 10);
            curr = curr -> next;
            if( p != NULL) p = p -> next;
            if( q != NULL) q = q -> next;
            carry = sum / 10;  
        }
        if( carry !=0) curr -> next = new ListNode(carry);  // * 这个容易忘
        return dummyHead -> next;
    }
};